package com.cheyunkeji.er.base;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cheyunkeji.er.MyFragmentManager;
import com.cheyunkeji.er.view.SLoadingDialog;
import com.umeng.analytics.MobclickAgent;

import androidx.fragment.app.Fragment;


/**
 * Created by Administrator on 2017/2/13.
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener {

    protected String TAG = BaseFragment.class.getSimpleName() ;

    protected View contentView;

    protected SLoadingDialog loadDialog;

    public View getContentView() {
        return contentView;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (contentView == null) {
            loadDialog = new SLoadingDialog(getActivity());
            doInflaterView(inflater, container);
            init();
            initView();
            initData();
        }
        TAG = this.getClass().getSimpleName();
        MyFragmentManager.getInstance().addFrag(this.getClass(), this);

        ViewGroup parent = (ViewGroup) contentView.getParent();
        if (parent != null) {
            parent.removeView(contentView);
        }
        return contentView;
    }

    protected boolean isVisible ;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if(getUserVisibleHint()){
            isVisible = true ;
            onVisible();
        }else{
            isVisible = false ;
            onInvisible();
        }
    }

    protected void onVisible(){
        lazyLoad();
        Log.e(TAG, "onVisible: Fragment onVisble executed" );
    }

    protected  void lazyLoad(){

    }

    protected void onInvisible(){}

    /**
     * inflate Content view
     *
     * @param inflater
     * @param container
     */
    protected abstract void doInflaterView(LayoutInflater inflater, ViewGroup container);

    protected abstract void init();

    protected abstract void initView();

    protected abstract void initData();

    public void showDialog() {
        if(!loadDialog.isShowing())
        loadDialog.show();
    }

    public void dismissDialog() {
        if( null != loadDialog && loadDialog.isShowing())
        loadDialog.dismiss();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        MyFragmentManager.getInstance().removeFrag(this.getClass());
    }



}
